package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.McMchntFeeDiscountTransDataMapper;
import cc.rengu.igas.momp.common.entity.McMchntFeeDiscountTransData;
import cc.rengu.igas.momp.common.entity.TMMcMchntInfo;
import cc.rengu.igas.momp.common.enums.McStatusEnum;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * @author liujinan
 * @version 1.0.0
 * @date 2020/5/6
 * @description 手续费接口实现
 */

public class McMchntFeeDiscountTransDataMapperImpl implements McMchntFeeDiscountTransDataMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 使用手续费优惠对象中的商户号mchntNo和商圈mchntArea查询商户手续费优惠营销活动信息表T_M_MC_MFD_INFO和
     * 营销活动商户信息表T_M_MC_MCHNT_INFO接口实现
     *
     * @param instId
     * @param mchntNo
     * @param mchntArea
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/5/6
     */
    @Override
    public McMchntFeeDiscountTransData selectMchntFeeDiscountActivityInfo(String instId, String mchntNo, String mchntArea) throws Exception {

        McMchntFeeDiscountTransData mcMchntFeeDiscountTransData = new McMchntFeeDiscountTransData();
        mcMchntFeeDiscountTransData.setInstId(instId);
        mcMchntFeeDiscountTransData.setMchntNo(mchntNo);
        mcMchntFeeDiscountTransData.setMchntArea(mchntArea);
        mcMchntFeeDiscountTransData.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);

        String sqlStr;
        if (null == mchntArea || "".equals(mchntArea)) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectMchntFeeDiscountActivityInfo");
        } else {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectMchntFeeDiscountActivityInfoWithMchntArea");
        }

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mcMchntFeeDiscountTransData, mcMchntFeeDiscountTransData);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>,商户<{}>或商圈<{}>,查询失败",
                    instId, mchntNo, mchntArea, iReturnCode);
            return null;
        }

        return mcMchntFeeDiscountTransData;
    }

    /**
     * 更新营销活动商户信息表T_M_MC_MCHNT_INFO中的退出标志exit_flag
     *
     * @param tmMcMchntInfo
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/5/9
     */
    @Override
    public int updateTMMcMchntInfo(TMMcMchntInfo tmMcMchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompUpdateTMMcMchntInfo");
        tmMcMchntInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcMchntInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新营销活动商户信息表T_M_MC_MCHNT_INFO失败！");
        }
        return iReturnCode;
    }
}
